package com.songcan.gateway.configuration;


import com.songcan.gateway.contants.AuthUrls;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

import java.net.URLDecoder;
import java.util.Collection;
import java.util.Map;

/***
 * 设置feignclient服务调用token头部令牌传送
 */
@Configuration
@Slf4j
public class FeignClientConfiguration  implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        Map<String, Collection<String>> queryMap = requestTemplate.queries();
        if(queryMap.keySet().contains(AuthUrls.AUTHORIZATION)){
            String token = queryMap.get(AuthUrls.AUTHORIZATION).toArray()[0].toString();
            if(!token.isEmpty()) {
                requestTemplate.header(HttpHeaders.AUTHORIZATION, URLDecoder.decode(token));
            }
        }
    }
}
